package com.lhl.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lhl.blog.common.exception.BlogException;
import com.lhl.blog.common.pojo.dataobject.BlogInfo;
import com.lhl.blog.common.pojo.dataobject.UserInfo;
import com.lhl.blog.common.pojo.request.UserLoginParam;
import com.lhl.blog.common.pojo.response.UserInfoResponse;
import com.lhl.blog.common.pojo.response.UserLoginResponse;
import com.lhl.blog.common.utils.BeanConver;
import com.lhl.blog.common.utils.JwtUtils;
import com.lhl.blog.common.utils.SecurityUtil;
import com.lhl.blog.model.BlogInfoMapper;
import com.lhl.blog.model.UserInfoMapper;
import com.lhl.blog.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.SimpleTimeZone;

@Service
public class UserServiceImp implements UserService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;
    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Override
    public UserLoginResponse login(UserLoginParam userParam) {
        //判断用户对否存在
        //根据用户名去数据库查询用户信息
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName, userParam.getUserName()).eq(UserInfo::getDeleteFlag, 0));
        if (userInfo==null){
            throw new BlogException("用户不存在");
        }
        //用户存在, 校验密码是否正确
        if (!SecurityUtil.verify(userParam.getPassword(), userInfo.getPassword())){
            throw new BlogException("密码错误");
        }
        //密码正确
        UserLoginResponse userLoginResponse = new UserLoginResponse();
        userLoginResponse.setUserId(userInfo.getId());
        //token里面的载荷信息
        Map<String,Object> map = new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name",userInfo.getUserName());
        userLoginResponse.setToken(JwtUtils.genToken(map));

        return userLoginResponse;
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        UserInfo userInfo = selectUserInfoById(userId);
        UserInfoResponse userInfoResponse = BeanConver.tans(userInfo);
        return userInfoResponse;
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        //根据博客id获取作者id
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag,0));
        if (blogInfo == null){
            throw new BlogException("博客不存在！");
        }

        Integer userId = blogInfo.getUserId();
        //根据作者id获取作者信息
        UserInfo userInfo = selectUserInfoById(userId);
        UserInfoResponse userInfoResponse = BeanConver.tans(userInfo);
        return userInfoResponse;
    }

    public UserInfo selectUserInfoById(Integer userId) {
      return   userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId,userId)
                .eq(UserInfo::getDeleteFlag,0));
    }
}
